﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;
using System.Xml;
using System.Xml.Serialization;
using TimeZoneTest.Data_Layer;
using TimeZoneTest.Model;
using TimeZoneTest.ViewModel.Base;

namespace TimeZoneTest.ViewModel
{
   using SLTimeZoneHelper;

   public class CrmViewModel : BaseNotify
   {
      private TimeZone currentZone;

      public IEnumerable<TimeZone> AvailableTimeZones
      {
         get { return _availableTimeZones; }
         set
         {
            _availableTimeZones = value;
            RaisePropertyChanged(() => AvailableTimeZones);
         }
      }

      private DateTime utcDateTime;
      public DateTime UTCDateTime
      {
         get { return utcDateTime; }
         set
         {
            utcDateTime = value;
            this.RaisePropertyChanged(() => UTCDateTime);
            this.RaisePropertyChanged(() => UTCTime);
         }
      }

      public DateTime UTCTime
      {
         get { return SelectedTimeZone == null ? DateTime.MaxValue : SelectedTimeZone.ConvertToLocal(DateTime.SpecifyKind(UTCDateTime, DateTimeKind.Utc)); }
      }

      public TimeZone SelectedTimeZone
      {
         get { return currentZone; }
         set
         {
            currentZone = value;
            RaisePropertyChanged(() => SelectedTimeZone);
            RaisePropertyChanged(() => UTCTime);
            RaisePropertyChanged(() => PCTime);
         }
      }

      private DateTime pcDateTime;
      private IEnumerable<TimeZone> _availableTimeZones;

      public DateTime PCDateTime
      {
         get { return pcDateTime; }
         set
         {
            pcDateTime = value;
            this.RaisePropertyChanged(() => PCDateTime);
            this.RaisePropertyChanged(() => PCTime);
            this.RaisePropertyChanged(() => RealUTCTime);
         }
      }

      public DateTime PCTime
      {
         get { return SelectedTimeZone == null ? DateTime.MaxValue : SelectedTimeZone.ConvertToLocal(PCDateTime.ToUniversalTime()); }
      }

      public DateTime RealUTCTime
      {
         get { return SelectedTimeZone == null ? DateTime.MaxValue : SelectedTimeZone.ConvertToUtc(PCDateTime); }
      }

      public ICommand GetZonesCommand { get; private set; }

      public CrmViewModel()
      {
			GetZonesCommand = new DelegateCommand<object>(GetAllZones, (o) =>

				{
					try
					{
						return SilverCrmSoap.Helpers.SoapHelper.ServerUri != null;
					}
					catch (Exception)
					{

						return false;
					}
					
				});
         
         PCDateTime = DateTime.Now;
         UTCDateTime = DateTime.Now;


         AvailableTimeZones = CommonZones.GetZones();
         SelectedTimeZone = AvailableTimeZones.First();
      }

      private void GetAllZones(object obj)
      {
         CrmProxy.Instance.GetAllTimeZones(GetAllZonesCompleted);
      }

      private void GetAllZonesCompleted(AsyncReponse<List<TimeZone>> response)
      {
         response.Result.Sort((zone, timeZone) => System.String.Compare(zone.UserInterfaceName, timeZone.UserInterfaceName, System.StringComparison.Ordinal));
         UI(() =>
            {
               AvailableTimeZones = response.Result;
               SelectedTimeZone = AvailableTimeZones.First();
            });
      }

      private void UI(Action action)
      {
         if (!Deployment.Current.Dispatcher.CheckAccess())
         {
            Deployment.Current.Dispatcher.BeginInvoke(action);
         }
         else
         {
            action();
         }
      }
   }

   public class StaticSurrogate
   {
      public DateTime Now { get { return DateTime.Now; } }
      public DateTime UTCNow { get { return DateTime.UtcNow; } }
   }

   public class Formatter : IValueConverter
   {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
         if (parameter != null)
         {
            string formatterString = parameter.ToString();

            if (!String.IsNullOrEmpty(formatterString))
            {
               return String.Format(culture, String.Format("{{0:{0}}}", formatterString), value);
            }
         }

         return (value ?? "").ToString();
      }

      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
         throw new NotImplementedException();
      }
   }
}

